package com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.listener.manual_load

import android.content.Context
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.ATNetworkConfirmInfo
import com.anythink.core.api.AdError
import com.anythink.interstitial.api.ATInterstitialExListener
import com.bp.hearthealth.pulse.lnapps.BpApplication
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsCount
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsSwitch
import com.bp.hearthealth.pulse.lnapps.ads.topon.interstitial_ad.TopOnInterAdManager
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.observer.SplashType
import com.bp.hearthealth.pulse.lnapps.ads.topon.open_ad.observer.TopOnSplashFinishObservable
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.DateUtils
import java.util.Timer
import java.util.TimerTask

class ATInterstitialExListenerImpl : ATInterstitialExListener {

    private var adManualLoadListener: TopOnInterstitialAdListener? = null
    private var onRefresh = true

    fun setManualLoadListener(topOnInterstitialAdListener: TopOnInterstitialAdListener?) {
//        LogUtil.iTopOn("【插页广告】总开关 : ${AdsSwitch.getBoolean(AdsSwitch.adInterControl)}")
        this.adManualLoadListener = topOnInterstitialAdListener
        if (!AdsSwitch.getBoolean(AdsSwitch.adInterControl)) {
            adManualLoadListener?.onFireBaseAdIsClosed()
            adManualLoadListener = null
        }
    }

    /**
     * 手动加载 deeplink回调，针对Adx、OnlineApi广告 isSuccess：是否成功
     */
    override fun onDeeplinkCallback(adInfo: ATAdInfo, isSuccess: Boolean) {
        adManualLoadListener?.onDeeplinkCallback(adInfo, isSuccess)
        LogUtil.iTopOn(
            "TopOn 【插页广告】 onDeeplinkCallback:$adInfo--status:$isSuccess"
        )
    }

    /**
     * 手动加载 应用类广告点击下载时回调方法
     */
    override fun onDownloadConfirm(
        context: Context?, adInfo: ATAdInfo, networkConfirmInfo: ATNetworkConfirmInfo?
    ) {
        adManualLoadListener?.onDownloadConfirm(context, adInfo, networkConfirmInfo)
        LogUtil.iTopOn(
            "TopOn 【插页广告】 onDownloadConfirm: adInfo=$adInfo"
        )
    }

    /**
     * 手动加载 广告加载成功回调
     */
    override fun onInterstitialAdLoaded() {
        onRefresh = true
        adManualLoadListener?.onInterstitialAdLoaded()
        LogUtil.iTopOn("TopOn 【插页广告】 onInterstitialAdLoaded")
        LogUtil.iTopOn("TopOn 【插页广告】 ~~~~ 【结束时间】： ${DateUtils.nowTime(System.currentTimeMillis())}")
    }

    /**
     * 手动加载 广告加载失败回调
     */
    override fun onInterstitialAdLoadFail(adError: AdError) {
        if (onRefresh) {
            onRefresh = false
            //延迟10秒加载
            Timer().schedule(object : TimerTask() {
                override fun run() {
                    TopOnInterAdManager.manualLoadAd()
                    LogUtil.iTopOn("TopOn 【插页广告】 加载失败 延迟10s后重新请求 插页广告。。。。。。。。")
                }
            }, 10000)
        }
        adManualLoadListener?.onInterstitialAdLoadFail(adError)
        LogUtil.iTopOn(
            """ TopOn   【插页广告】 onInterstitialAdLoadFail:
     ${adError.fullErrorInfo}
     """.trimIndent()
        )
    }

    /**
     * 手动加载 广告被点击回调
     */
    override fun onInterstitialAdClicked(entity: ATAdInfo) {
        AdsCount.incrementClickCount()
        adManualLoadListener?.onInterstitialAdClicked(entity)
        LogUtil.iTopOn(
            "TopOn 【插页广告】 onInterstitialAdClicked 广告来源：${entity.networkName}"
        )
    }

    /**
     * 手动加载 广告展示回调 建议在此回调中调用load进行广告的加载，方便下一次广告的展示
     */
    override fun onInterstitialAdShow(entity: ATAdInfo) {
        AdsCount.incrementDisplayCount()
        BpApplication.adIsShow = true
        adManualLoadListener?.onInterstitialAdShow(entity)
        LogUtil.iTopOn(
            "TopOn 【插页广告】 onInterstitialAdShow  广告来源：${entity.networkName}"
        )
    }

    /**
     * 手动加载 广告关闭回调
     */
    override fun onInterstitialAdClose(entity: ATAdInfo) {
        BpApplication.adIsShow = false
        adManualLoadListener?.onInterstitialAdClose(entity)
        if (AdsSwitch.getBoolean(AdsSwitch.adInterCacheControl)) {
            TopOnInterAdManager.manualLoadAd()
        }
        LogUtil.iTopOn(
            "TopOn 【插页广告】 onInterstitialAdClose 广告来源：${entity.networkName}"
        )
    }

    /**
     * 手动加载 视频广告开始播放回调
     */
    override fun onInterstitialAdVideoStart(entity: ATAdInfo) {
        adManualLoadListener?.onInterstitialAdVideoStart(entity)
        LogUtil.iTopOn(
            "TopOn 【插页广告】 onInterstitialAdVideoStart 广告来源：${entity.networkName}"
        )
    }

    /**
     * 手动加载 视频广告播放结束回调
     */
    override fun onInterstitialAdVideoEnd(entity: ATAdInfo) {
        adManualLoadListener?.onInterstitialAdVideoEnd(entity)
        LogUtil.iTopOn(
            "TopOn 【插页广告】 onInterstitialAdVideoEnd 广告来源：${entity.networkName}"
        )
    }

    /**
     * 手动加载 视频广告播放失败回调
     */
    override fun onInterstitialAdVideoError(adError: AdError) {
        adManualLoadListener?.onInterstitialAdVideoError(adError)
        LogUtil.iTopOn(
            """ TopOn 【插页广告】 onInterstitialAdVideoError:
     ${adError.fullErrorInfo}
     """.trimIndent()
        )
    }
}